#include <iostream>
#include <algorithm>
#include <cstring>
//849
using namespace std;

const int N = 510;

int n, m;
int graph[N][N];
int dist[N];
bool state[N];

int dijkstra(){
    dist[1] = 0;


    for (int i = 0; i < n; i ++){
        int t = -1;
        for (int j = 1; j <= n; j ++){
            if(!state[j] && (t == -1 || dist[t] > dist[j])){
                t = j;
            }
        }

        state[t] = true;
        for (int j = 1; j <= n; j ++){
            dist[j] = min(dist[j], dist[t] + graph[t][j]);
        }
    }

    if(dist[n] == 0x3f3f3f3f){
        return -1;
    }else{
        return dist[n];
    }
}

int main()
{
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);

    cin >> n >> m;

    memset(graph, 0x3f, sizeof graph);
    memset(dist, 0x3f, sizeof dist);

    for (int i = 1; i <= n; i ++){
        graph[i][i] = 0;
    }

    for (int i = 0; i < m; i++)
    {
        int a, b, c;
        cin >> a >> b >> c;

        graph[a][b] = min(graph[a][b], c);
    }


    cout << dijkstra() << endl;

    return 0;
}